package net.cctms.zhishu.webAppFramework.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.io.File;

/**
 * 图片加载工具类
 * Created by yf on 2017/8/10.
 */
public class GlideUtil {
    public static DisplayImageOptions mBrowsePictureOptions;

    static {
        BitmapFactory.Options decodingOptions = new BitmapFactory.Options();
        mBrowsePictureOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .decodingOptions(decodingOptions)
                .imageScaleType(ImageScaleType.NONE)
                .build();
    }





    /**
     * 以bitmap形式下载
     * <p>
     * use: 图集
     *
     * @param context  上下文实例
     * @param url      图片链接
     * @param listener 下载监听
     */
    public static void loadGif(Context context, String url, ImageView imageView, RequestListener<String, GlideDrawable> listener) {
        try{
            if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
                return;
            }
            Glide.with(context).load(url)
                    .listener(listener)
                    .into(imageView);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 以bitmap形式下载
     * <p>
     * use: 图集
     *
     * @param context 上下文实例
     * @param url     图片链接
     * @param target  下载监听
     */
    public static void downloadBitmap(Context context, String url, SimpleTarget<Bitmap> target) {
        if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
            return;
        }
        url = ImgUtils.convertWebpSuffix(url); //转换Webp格式
        Glide.with(context).load(url).asBitmap().into(target);
    }

    /**
     * 图集加载图片
     *
     * @param context
     * @param url
     * @param listener
     */
    public static void downloadBitmap(Context context, String url, SimpleImageLoadingListener listener) {
        if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
            return;
        }
        url = ImgUtils.convertWebpSuffix(url); //转换Webp格式
      // Glide.with(context).load(url).into(imageView);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .writeDebugLogs()
                .defaultDisplayImageOptions(mBrowsePictureOptions)
                .diskCacheExtraOptions(480, 320, null)
                .build();
        ImageLoader.getInstance().init(config);
        ImageLoader.getInstance().loadImage(url, mBrowsePictureOptions, listener);
    }


    /**
     * 以bitmap形式下载
     *
     * @param context 上下文实例
     * @param url     下载监听
     */
    @Deprecated
    public static void downloadBitmap(Context context, String url) {
        if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
            return;
        }
        url = ImgUtils.convertWebpSuffix(url); //转换Webp格式
        Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

            }
        });
    }

    /**
     * 以file形式下载
     *
     * @param context
     * @param url
     * @param target
     */
    public static void downloadFile(Context context, String url, SimpleTarget<File> target) {
        if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
            return;
        }
        url = ImgUtils.convertWebpSuffix(url); //转换Webp格式
        Glide.with(context).load(url).downloadOnly(target);
    }

    /**
     * 以file形式下载
     *
     * @param context
     * @param url
     */
    public static void downloadFile(Context context, String url, int width, int height) {
        if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
            return;
        }
        url = ImgUtils.convertWebpSuffix(url); //转换Webp格式
        Glide.with(context).load(url).downloadOnly(new SimpleTarget<File>(width, height) {
            @Override
            public void onResourceReady(File file, GlideAnimation<? super File> glideAnimation) {

            }
        });
    }


    /**
     * 加载图片
     *
     * @param context
     * @param imgUrl
     * @param v
     */
    public static void loadImage(Context context, String imgUrl, ImageView v, int defaultImg) {
        if ((context instanceof Activity) && ((Activity) context).isFinishing()) {
            return;
        }

        try{
            imgUrl = ImgUtils.convertWebpSuffix(imgUrl); //转换Webp格式
            Glide.with(context).load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(defaultImg)
                    .error(defaultImg)
                    .dontAnimate()
                    .into(v);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}

